/*
 * @Author: ShiJunJie
 * @Date: 2022-03-30 11:49:19
 * @LastEditors  : ShiJunJie
 * @LastEditTime : 2022-05-09 11:27:51
 * @Descripttion: 
 */
/**
 * =========================================================================
 * 扩展插件作用：vue模块自动化按需引入，源码来源 https://github.com/antfu
 * ----------------------------------------------------------------------
 * 支持以下模块自动引用，无需再使用 import * from ***
 * NaiveUi、AntDesign、Element、pinia、vue-composition-api、vue-i18n、vue-router、@vueuse/core等...
 * 当然，你习惯手工引用也是可以的，并且开发界面项目建议使用 NaiveUi 框架，真心不错
 * =========================================================================
 */
import { resolve } from 'path'
import { version as vueVersion } from 'vue/package.json'
import vueUseImports from './vueuse-core'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import PkgConfig from 'vite-plugin-package-config'
import OptimizationPersist from 'vite-plugin-optimize-persist'
import IconsResolver from 'unplugin-icons/resolver'

import { VueUseComponentsResolver } from 'unplugin-vue-components/resolvers'

// 选择合适你项目的UI分解器放进扩展配置，默认 vue2 使用 ElementUiResolver, vue3 使用 NaiveUiResolver
import { NaiveUiResolver, ElementPlusResolver, ElementUiResolver, AntDesignVueResolver, ArcoResolver } from 'unplugin-vue-components/resolvers'

export default (options?: viteUserOptions): viteUserPlugins => {
  const isVue2 = parseInt(vueVersion) < 3
  const plugins = [
    // 从 package.json 文件内读取vite预构建依赖清单，自动实现 optimizeDeps: { include: [] } 配置
    // see https://www.npmjs.com/package/vite-plugin-package-config
    PkgConfig(),

    // 强制预构建依赖，加快启动速度，防止修改 vite.config.ts 配置重启时自动化获取所需依赖缓慢而导致出错
    // see https://www.npmjs.com/package/vite-plugin-optimize-persist
    OptimizationPersist(),

    // [import自动引入](https://www.npmjs.com/package/unplugin-auto-import)
    AutoImport({
      dts: resolve(process.cwd(), './config/types/auto-imports.d.ts'),
      imports: [isVue2 ? '@vue/composition-api' : 'vue', 'pinia', 'vue-i18n', 'vue-router', vueUseImports],
      resolvers: [isVue2 ? ElementUiResolver() : NaiveUiResolver(), VueUseComponentsResolver()],
      // 解决eslint报错
      eslintrc: {
        enabled: true, // Default `false` 为true时，会根据filepath生成一个eslint的配置文件, 这个文件需要引入到eslint的配置文件中
        filepath: resolve(process.cwd(), './node_modules/.eslintrc-auto-import.json'), // Default `./.eslintrc-auto-import.json`
        globalsPropValue: true // Default `true`, (true | false | 'readonly' | 'readable' | 'writable' | 'writeable')
      }
    }),

    // [组件按需自动引入](https://www.npmjs.com/package/unplugin-vue-components)
    Components({
      directives: false,
      transformer: isVue2 ? 'vue2' : 'vue3',
      extensions: ['vue', 'md', 'tsx'],
      include: isVue2 ? [] : [/\.vue$/, /\.md$/, /\.tsx$/],
      dts: resolve(process.cwd(), './config/types/components.d.ts'),
      resolvers: [
        IconsResolver({
          customCollections: ['svg', 'my'],
          componentPrefix: 'Icon' // 图标标签别名前缀
        }),
        isVue2 ? ElementUiResolver() : AntDesignVueResolver(),
        VueUseComponentsResolver()
      ]
    }),

  ]

  return { plugins }
}
